"use strict";
var _configCenter = _interopRequireDefault(require("../../config-center"));
var _parser = _interopRequireDefault(require("./parser"));
function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const initSearchParser = (parserMap)=>{
    const mapper = [];
    parserMap.forEach((value)=>{
        if (value.canSearch) {
            mapper.push(value);
        }
    });
    return mapper;
};
/** 默认 url 都是经过 supported site 判断的 */ let NovelServices = class NovelServices {
    async searchBook(keyword) {
        const workArr = this.searchParsers.map((parser)=>parser.search(keyword).catch(()=>[]
            )
        );
        const resultArr = await Promise.all(workArr);
        const result = [];
        const nameMap = new Map();
        let ptr = 0;
        resultArr.forEach((items)=>items.forEach((item)=>{
                const { bookName , bookUrl , author  } = item;
                // 如果是当前不支持的书源就直接滤掉
                if (!this.supportedSites.some((site)=>bookUrl.includes(site)
                )) {
                    return;
                }
                const uniqueKey = `${bookName}${author}`;
                const position = nameMap.get(uniqueKey);
                if (position == null) {
                    nameMap.set(uniqueKey, ptr);
                    result[ptr++] = {
                        bookName,
                        author,
                        plantformId: 0,
                        source: [
                            bookUrl
                        ]
                    };
                } else {
                    result[position].source.push(bookUrl);
                }
            })
        );
        return result;
    }
    /** 获取书籍目录 */ async analyzeList(url) {
        return this.getParser(url).getChapterList(url);
    }
    /** 获取章节内容 */ async analyzeChapter(url) {
        return this.getParser(url).getChapterDetail(url);
    }
    /** 获取最新章节 */ async analyzeLatestChapter(url) {
        return this.getParser(url).getLatestChapter(url);
    }
    /** 批量获取最新章节 */ async analyzeLatestChapters(list) {
        const resLst = await Promise.all(list.map((i)=>this.analyzeLatestChapter(i.url).catch(()=>null
            )
        ));
        const workQueue = [];
        const markList = [];
        const result = resLst.map((item, index)=>{
            const listItem = list[index];
            if (item != null && item !== listItem.title) {
                const catalogUrl = listItem.catalogUrl || listItem.url;
                workQueue.push(this.analyzeList(catalogUrl).catch(()=>null
                ));
                markList.push(index);
                return {
                    title: item,
                    list: []
                };
            } else {
                return null;
            }
        });
        const lists = await Promise.all(workQueue);
        lists.forEach((curList)=>{
            const index = markList.shift();
            if (curList != null) {
                result[index].list = curList;
            } else {
                result[index] = null;
            }
        });
        return result;
    }
    /** 获取书籍信息 */ async getBookInfo(url) {
        return this.getParser(url).getBookDetail(url);
    }
    /** 批量获取书籍信息，包括最新章节 */ async getBookInfos(sources) {
        const stamp = Date.now();
        const workArr = sources.map((url, index)=>this.getBookInfo(url).then((info)=>{
                info.plantformId = index;
                info.stamp = Date.now() - stamp;
                return info;
            }).catch(()=>null
            )
        );
        const results = await Promise.all(workArr);
        return results.filter((i)=>Boolean(i)
        ).sort((a, b)=>a.stamp - b.stamp
        );
    }
    /** 获取书源信息 */ async getOrigin(catalogUrls) {
        const workQueue = catalogUrls.map((item)=>this.getBookInfo(item).catch(()=>null
            )
        );
        const resLst = await Promise.all(workQueue);
        const result = resLst.filter((i)=>Boolean(i)
        ).map((i, ind)=>({
                catalogUrl: i.catalogUrl,
                url: catalogUrls[ind],
                latestChapter: i.latest || '获取失败'
            })
        );
        return result;
    }
    constructor(){
        this.init = ()=>{
            this.supportedSites = _configCenter.default.supportedSites;
            this.parserMap = new Map();
            _configCenter.default.configs.forEach((value, key)=>{
                this.parserMap.set(key, new _parser.default(value));
            });
            this.searchParsers = initSearchParser(this.parserMap);
        };
        this.getParser = (url)=>{
            const currentSite = this.supportedSites.find((i)=>url.includes(i)
            );
            return this.parserMap.get(currentSite);
        };
        this.init();
        _configCenter.default.addConfigListener(this.init);
    }
};
module.exports = NovelServices;
